<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/27
 * Time: 17:24
 */

namespace common\components;


use yii\helpers\Url;
use yii\web\UrlManager;

class GlobalUrlComponent
{
    public static function buildWwwUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['www'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_FRONTEND ) {
            $routerFile = \Yii::getAlias("@frontend") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }

            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }


    public static function buildUrl($app, $path, $params=[], $isSuffix = true)
    {
        if ( $app == DataHelper::APP_STATIC ) {
            return self::buildStaticUrl($path, $params);
        }
        $domain = DataHelper::getConfig('domains')[$app];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != $app ) {
            $routerFile = \Yii::getAlias("@$app") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildStaticUrl($path, $params=[])
    {
        $domain = DataHelper::getConfig('domains')['static'];

        if( stripos($domain,"http") === false ) {
            $domain = "http://".$domain;
        }

        Url::$urlManager = \Yii::createObject([
            "class" => UrlManager::class,
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
        ]);

        $path = Url::to(array_merge([$path], $params));
        Url::$urlManager = null;

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }
    public static function buildAdminUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['admin'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_BACKEND ) {
            $routerFile = \Yii::getAlias("@admin") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildBackendUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['backend'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_BACKEND ) {
            $routerFile = \Yii::getAlias("@backend") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildAgentUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['agent'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_BACKEND ) {
            $routerFile = \Yii::getAlias("@agent") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildCompanyUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['company'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_BACKEND ) {
            $routerFile = \Yii::getAlias("@manage") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildFrontendUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['frontend'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_FRONTEND ) {
            $routerFile = \Yii::getAlias("@frontend") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildNetDeskTopUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['net_desktop'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_BACKEND ) {
            $routerFile = \Yii::getAlias("@backend") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }

            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildApiUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['api'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_API ) {
            $routerFile = \Yii::getAlias("@api") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }
            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildChainUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['chain'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_CHAIN ) {
            $routerFile = \Yii::getAlias("@chain") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }
            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }

    public static function buildEmployeeUrl($path, $params=[], $isSuffix = true)
    {
        $domain = DataHelper::getConfig('domains')['employee'];

        if( stripos($domain,"http") === false )
        {
            $domain = "http://".$domain;
        }

        if ( DataHelper::getCurrentApp() != DataHelper::APP_EMPLOYEE ) {
            $routerFile = \Yii::getAlias("@employee") . DIRECTORY_SEPARATOR . "config/router.php";
            if ( is_file($routerFile) ) {
                $routerConfig = include $routerFile;
                $routerConfig["class"] = UrlManager::class;
                $urlManager = \Yii::createObject($routerConfig);
                Url::$urlManager = $urlManager;
            }
            //路由中带点的情况也就是有用自定义后缀覆盖原有后缀
            if ( (Url::$urlManager->suffix && strpos($path, ".") !== false) || !$isSuffix ) {
                Url::$urlManager->suffix = "";
            }
            $path = Url::to(array_merge([$path], $params));
            Url::$urlManager = null;
        } else {
            $path = Url::to(array_merge([$path], $params));
        }

        return rtrim($domain, "/") . "/" . ltrim($path, "/");
    }
    public static function buildNullUrl()
    {
        return "javascript:void(0);";
    }
}